package rursu.dal.managers;

import rursu.dal.managers.interfaces.IMovieSearchDAL;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MovieSearchDALManager implements IMovieSearchDAL {

	public static final String TABLE_SEARCH_NAME = "search";
	
	public static final String ID = "_id";
	public static final String QUERY = "query";
	
	private SQLiteDatabase db;
	
	public MovieSearchDALManager(SQLiteDatabase db) {
		this.db = db;
	}

	public void insert(String query) {
		ContentValues values = new ContentValues();

        values.put( MovieSearchDALManager.QUERY, query);

        db.insert(TABLE_SEARCH_NAME, null, values);		
	}
  
	public int getId(String query) {
		Cursor cursor = db.query(TABLE_SEARCH_NAME, new String [] {		
				MovieSearchDALManager.ID	},
				MovieSearchDALManager.QUERY + "=" + query, null, null, null, null, "1");

		int answer = -1;
		
		if ( cursor.getCount() > 0 ) {
			
			cursor.moveToFirst();
			
			answer = cursor.getInt(cursor.getColumnIndex(MovieSearchDALManager.ID));
		}
			
		cursor.close();
		return answer;
	}

	public int delete(String query) {
		return db.delete(TABLE_SEARCH_NAME, MovieSearchDALManager.QUERY + "=\"" + query+"\"", null);
	}

	public String getQuery() {
		
		String query = null;
		
//		Cursor cursor = db.query(TABLE_SEARCH_NAME, new String [] {		
//				MovieSearchDALManager.QUERY	}, null, null, null, null, null);
		
		Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_SEARCH_NAME, null);
		
		if ( cursor.getCount() > 0 ) {
			
			cursor.moveToFirst();
			
			query = cursor.getString(cursor.getColumnIndex(MovieSearchDALManager.QUERY));
		}
			
		cursor.close();
		return query;
	}

	
}
